import 'dart:async';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../news_model.dart';
import '../home_page_viewmodel.dart';

class StoriesNotifier extends AsyncNotifier<List<NewsListModel>> {
  final HomePageViewModel viewModel;

  StoriesNotifier({required this.viewModel});

  @override
  FutureOr<List<NewsListModel>> build() async {
    if (viewModel.stories.isNotEmpty) {
      return viewModel.stories;
    }
    await viewModel.fetchLastest();
    return viewModel.stories;
  }

  Future<void> loadMore() async {
    await viewModel.fetchBefore();
    state = AsyncData(viewModel.stories);
  }
}